import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { TagDocument } from './shema/tag.shema';

@Injectable()
export class TagService {
  constructor(@InjectModel('Tag') private readonly tagModel:Model<TagDocument>){}
  // create-tag
  async create(createTagDto: CreateTagDto):Promise<TagDocument>{
    return await this.tagModel.create(createTagDto);
  }
  // get all tags
  async findAll():Promise<TagDocument[]>{
    return await this.tagModel.find().exec();
  }

  // get tag by name
  async findOneByTagName(tag:string):Promise<TagDocument>{
    return await this.tagModel.findOne({tag:tag})
  }

  async findOne(type: string){
    return this.tagModel.findOne({type:type}).exec();
  }
  // update-tag
  async update(updateTagDto: UpdateTagDto) {
    return (await this.tagModel.updateOne({_id:updateTagDto.id},{$set:updateTagDto}).exec()).acknowledged;
  }
  // delete - tag
  async deleteOne(id: string):Promise<boolean>{
    return (await this.tagModel.deleteOne({_id:id}).exec()).acknowledged;
  }
}
